//
//  RSSParser.m
//  InTheSnow
//
//  Created by di on 7/24/09.
//  Copyright 2009 INTELLECTSOFT. All rights reserved.
//

#import "News.h"
#import "RSSParser.h"

enum  {
	kTitle = 0,
	kBody, 
	kCategory
};

@implementation RSSParser

@synthesize isUrl;
@synthesize property; 
@synthesize currentNews;
@synthesize author;
@synthesize title;
@synthesize description; 

- (id) initWithUrl:(NSURL*)url dictIndex:(int)ind
{
	self = [super init];
	if (self != nil) 
	{
		dictIndex = ind;
		self.isUrl = url;
	}
	return self;
}

- (void)parseRss
{
	/*	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	 
	 NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.inthesnow.com/index.php?id=47"]];
	 [parser setDelegate:self]; 
	 //	[parser setShouldProcessNamespaces:NO];
	 //	[parser setShouldReportNamespacePrefixes:NO];
	 //	[parser setShouldResolveExternalEntities:YES];
	 
	 [parser parse];	
	 
	 
	 
	 [parser release]; 
	 [pool release];
	 */	
}

- (void) parseData:(NSData*)dt
{
 
	for (News *n in __delegate.rssContentEvents) {
		[n release];
	}
	[__delegate.rssContentEvents removeAllObjects];
	
	[[NSURLCache sharedURLCache] setMemoryCapacity:0];
	[[NSURLCache sharedURLCache] setDiskCapacity:0];
	
	NSXMLParser *parser = [[NSXMLParser alloc] initWithData:dt] ;
	[parser setDelegate:self]; 
	
	[parser parse];		
	
	self.currentNews = nil;
	self.author = nil;
	self.description = nil; 
	[parser release];
	
	[[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:nil];
	
}

- (void)parser:(NSXMLParser*)p didStartElement:(NSString*)elementName namespaceURI:(NSString*)n qualifiedName:(NSString*)qName attributes:(NSDictionary*)attributeDict
{ 
	if (qName)
	{
		elementName = qName;
	}	    
	
	if([elementName isEqualToString:@"news"])
	{ 
		self.currentNews = [[News alloc] init] ;
		
		NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
		[formatter setDateFormat:@"dd.MM.yy  HH:mm"];
		
		
		currentNews.date = [formatter dateFromString:(NSString*)[attributeDict objectForKey:@"date"]];
		
		self.property = -7;
		[formatter release];		
		return;
	}
	
	if([elementName isEqualToString:@"title"]){  
		self.property = kTitle;
		return;
	}
	if([elementName isEqualToString:@"body"]){   
		self.property = kBody;
		return;
	}
	if([elementName isEqualToString:@"category"]){   
		self.property = kCategory;
		return;
	}
	if([elementName isEqualToString:@"image"]){  
		NSString *tmpBigImg = [[attributeDict objectForKey:@"bigImage"] stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
		currentNews.bigImage   = [NSURL URLWithString: tmpBigImg];
		NSString *tmpThumbImg = [[attributeDict objectForKey:@"ThumbImage"] stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
		currentNews.thumbImage = [NSURL URLWithString:tmpThumbImg ];//ThumbImage
		//		NSLog([[attributeDict objectForKey:@"ThumbImage"] description]);
	    self.property = -7;
		return;
	}
	
	
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
		NSLog(@" ERROR  %i - %i", [parser lineNumber], [parser columnNumber]);
		NSLog(@"domain %@, user info %@", [parseError domain], [parseError userInfo]);
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
	if (qName)
	{
		elementName = qName;
	}	 
	
	if([elementName isEqualToString:@"news"])
	{
		//	[[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:nil];		
	}
}

- (void)parserDidEndDocument:(NSXMLParser *)parser
{
	[[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:nil];
	[[NSNotificationCenter defaultCenter] postNotificationName:@"norotate" object:nil];
	//	NSLog(@"%i",[[__delegate rssContentNews] count]);
	
//	[[__delegate rssContent] insertObject:self.currentNews atIndex:dictIndex];
}

- (void)parserDidStartDocument:(NSXMLParser *)parser
{
	
	
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{ 		
	if (self.property == kTitle) {
		currentNews.title = [self.currentNews.title stringByAppendingString:string];
	}
	else if(self.property == kBody){
		currentNews.description = [self.currentNews.description stringByAppendingString:string];
	}
	else if (self.property == kCategory) {
		if ([string isEqualToString:@"Events"])
		{
			[[__delegate rssContentEvents] addObject:self.currentNews];  
		}
		else if ([string isEqualToString:@"Resorts"]) 
		{
			[[__delegate rssContentResorts] addObject:self.currentNews];  
		}
		else if ([string isEqualToString:@"Gear"])
		{
			[[__delegate rssContentGear] addObject:self.currentNews];  
		}
		else if ([string isEqualToString:@""])
		{
			
		}
	}
}

- (void) dealloc
{
	//	NSLog(@"[parser dealloc]"); 
	[isUrl release];
	[currentNews release];
	[author release];
	[title release];
 	[description release]; 
	[super dealloc];
}

@end
